﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using Traceability.Bais.API.Appliction.Command.Permissions;
using Traceability.Bais.API.Appliction.Command.Roless;
using Traceability.Bais.API.Appliction.Command.Users;
using Traceability.Common.Domain;
using Traceability.Common.Dtos;

namespace Traceability.Bais.API.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private  IMediator _mediator;
        private readonly ILogger<UsersController> _logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="logger"></param>
        public UsersController(IMediator mediator, ILogger<UsersController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet("LoginUser")]
        public async Task<Result<User>> LoginUser([FromQuery]UserListCommon enm)
        {
            try
            {
                _logger.LogInformation("登录日志测试");
                var list = await _mediator.Send(enm);
                return list;
            }
            catch (Exception me)
            {
                _logger.LogError("登录日志错误"+ me.Message);
                throw;
            }

            
        }
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <param name="enm"></param>
        /// <returns></returns>
        [HttpGet("GetPermissionList")]
        public async Task<Result<List<PremissionDto>>> GetPermissionList([FromQuery] PermissionListCommon enm)
        {
            var list= await _mediator.Send(enm);
            return list;
        }
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <param name="enm"></param>
        /// <returns></returns>
        [HttpGet("DeletePermission")]
        public async Task<Result> DeletePermission([FromQuery] PermissionDeleteCommon enm)
        {
            var list = await _mediator.Send(enm);
            return list;
        }
        /// <summary>
        /// 获取权限列表级联
        /// </summary>
        /// <param name="enm"></param>
        /// <returns></returns>
        [HttpGet("GetPermissionCascaderList")]
        public async Task<Result<List<PermissionCascaderDto>>> GetPermissionCascaderList()
        {
            var list= await _mediator.Send(new PermissionCascaderCommon());
            return list;
             
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="enm"></param>
        /// <returns></returns>
        [HttpPost("AddPermission")]
        public  async Task<Result> AddPermission(  PermissionCreateCommon enm)
        {
            var list= await _mediator.Send(enm);
            return list;
        }
        /// <summary>
        /// 获取角色权限列表
        /// </summary>
        /// <param name="enm"></param>
        /// <returns></returns>
        [HttpGet("GetRRolesPermission")]
        public async Task<Result<List<RolePermissionDto>>> GetRRolesPermission([FromQuery]RolesPermissionCommon enm)
        {
            var list = await _mediator.Send(enm);
            return list;
        }

        [HttpPost("AddRolesPermission")]
        public async Task<Result> AddRolesPermission([FromBody]RolesCreateCommon enm)
        {
            var list = await _mediator.Send(enm);
            return list;

        }

    }
}
